﻿/********************************************************************************/
// <copyright file="AdminFactory.cs" company="Asia E-Business Solutions">
//     Copyright © 2012. All right reserved
// </copyright>
// <history>
//     <change who="Phuoc Le" date="21/12/2012 5:05:54 PM">Created</change>
// </history>
/********************************************************************************/

using System;
using System.Collections.Generic;
using LINQ = JLL.China.Infrastructure.Repositories.LINQ2SQL;
using JLL.China.Infrastructure.Repositories.MembserGroup;

namespace JLL.China.Infrastructure.Repositories.Admin
{
    public static class AdminFactory
    {
        public static Model.Admin FromLINQObject(LINQ.Admin admin)
        {
            if (admin == null)
                return null;
            else
            {
                var model = new Model.Admin
                {
                    ID = admin.Admin_ID,
                    UserName = admin.UserName,
                    Password = admin.Password,
                    IsSupperAdmin = admin.IsSupperAdmin,
                    IsActive = admin.IsActive,
                    Role = admin.Role,
                    Group = MemberGroupFactory.FromLINQObject(admin.MemberGroup),
                    Modules = new List<Model.Module>()
                };

                if (admin.ModuleAccessRights != null && admin.ModuleAccessRights.Count > 0 && admin.ModuleAccessRights.HasLoadedOrAssignedValues)
                {
                    foreach (var m in admin.ModuleAccessRights)
                    {
                        var module = ModuleFactory.FromLINQObject(m.Module);
                        module.Permission = m.Permission;
                        model.Modules.Add(module);
                    }
                }
                return model;
            }
        }

        public static LINQ.Admin ToLINQObject(Model.Admin admin)
        {
            var model = new LINQ.Admin
            {
                Admin_ID = admin.ID,
                UserName = admin.UserName,
                Password = admin.Password,
                IsSupperAdmin = admin.IsSupperAdmin,
                IsActive = admin.IsActive,
                Role = admin.Role,
                GroupFID = admin.Group.ID
            };
            return model;
        }
    }
}
